// pages/user/list/list.js
const app = getApp();
var that = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userList:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;

    //获取用户信息：
    wx.request({
      url: app.globalData.host +'/usermgr/user/find',
      success: function (res) {
        console.log(res)
        var data = res.data;
        for(var i in data ){
          data[i].check=false;
        }
        that.setData({
          userList: data
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  changestatus : function (event){
    var i = event.currentTarget.dataset.idx;
    this.data.userList[i].check = !this.data.userList[i].check;
    console.log(this.data);
  },
  del: function(){
   var that = this;
    //获取被选中的id
   var uList =  this.data.userList;
   var ids = "";
   for(var i in uList){
      if(uList[i].check){
        ids = ids +","+uList[i].id;
      }
   }
   if (ids) {
     wx.showModal({
       title: '删除提示',
       content: '确认要删除用户吗？',
       success: function (res) {
         if (res.confirm) {
           ids = ids.substring(1)
           wx.request({
             url: app.globalData.host +'/usermgr/user/del/' + ids,
             dataType: "json",
             success: function (res) {
               if (res.data.status) {
                 wx.showToast({
                   title: '删除成功',
                   icon: 'success',
                   duration: 1000
                 })
                 that.onLoad();//重新加载页面
               }
             }
           })
         }
       }
     })
   }else{
     //提示请选择要删除的用户
     wx.showToast({
       title: '请选择用户',
       icon: 'none',
       duration: 2000
     })
   }
  },
  add:function(){
    wx.navigateTo({
      url: '/pages/user/add/add',
    })
  },
  update: function () {

    var that = this;
    //获取被选中的id
    var uList = this.data.userList;
    var ids = "";
    var count = 0;
    for (var i in uList) {
      if (uList[i].check) {
        ids = ids + "," + uList[i].id;
        count = count+1;
      }
    }
    if (ids && count==1) {
      ids = ids.substring(1)
      wx.navigateTo({
        url: '/pages/user/update/update?uid=' + ids,
      })
    } else {
      //提示请选择要删除的用户
      wx.showToast({
        title: '请选择一个用户',
        icon: 'none',
        duration: 2000
      })
    }
  }
})